package com.com.ruanmeng.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class Bitmap2Base64 {

	
	/**
	 * bitmapתΪbase64
	 * @param bitmap
	 * @return
	 */
	public static String bitmapToBase64(Bitmap bitmap) {

		String result = null;
		ByteArrayOutputStream baos = null;
		try {
			if (bitmap != null) {
				baos = new ByteArrayOutputStream();
				bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);

				baos.flush();
				baos.close();

				byte[] bitmapBytes = baos.toByteArray();
				result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (baos != null) {
					baos.flush();
					baos.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	/**
	 * base64תΪbitmap
	 * @param base64Data
	 * @return
	 */
	public static Bitmap base64ToBitmap(String base64Data) {
		byte[] bytes = Base64.decode(base64Data, Base64.DEFAULT);
		return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
	}
	 public static Bitmap convertStringToIcon(String st)
	    {  
	        // OutputStream out;  
	        Bitmap bitmap = null;  
	        try  
	        {  
	            // out = new FileOutputStream("/sdcard/aa.jpg");  
	            byte[] bitmapArray;  
	            bitmapArray = Base64.decode(st, Base64.DEFAULT);  
	            bitmap =  
	                    BitmapFactory.decodeByteArray(bitmapArray, 0,  
	                            bitmapArray.length);  
	            // bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);  
	            return bitmap;  
	        }  
	        catch (Exception e)  
	        {  
	            return null;  
	        }  
	    }  
}
